
#include "StdAfx.h"
#include "MonitorMessage.h"
#include "WorktableManager.h"

///////////////////////////////////////////////////////////
// CWorktableManager

CWorktableManager::CWorktableManager(void)
	: m_pActiveWorktable(NULL)
{
}

CWorktableManager::~CWorktableManager(void)
{
}

CMonitorWorktable* CWorktableManager::GetWorktable(DWORD dwID)
{
	CMonitorWorktable* pWorktable = NULL;

	CWorktableMap::iterator it = m_WorktableMap.find(dwID);

	if(it == m_WorktableMap.end())
		return NULL;
	else
		return it->second;
}

void CWorktableManager::RegisterWorktable(CMonitorWorktable *pWorktable)
{
	m_WorktableMap[pWorktable->m_dwID] = pWorktable;
	//if(m_pActiveWorktable == NULL)
	//{
	//	SetActiveWorktable(pWorktable->m_dwID);
	//}
}

void CWorktableManager::SetActiveWorktable(DWORD dwID)
{
	CMonitorWorktable* pWorktable = GetWorktable(dwID);

	if(pWorktable && m_pActiveWorktable != pWorktable)
	{
		DWORD dwInactiveID = 0;

		pWorktable->SetActive(TRUE);

		if(m_pActiveWorktable)
		{
			dwInactiveID = m_pActiveWorktable->m_dwID;
			m_pActiveWorktable->SetActive(FALSE);
		}

		m_pActiveWorktable = pWorktable;
	
		CWorktableMessage Message(ACTIVE_WORKTABLE_CHANGED, this);
		Message.m_dwActiveWorktableID = m_pActiveWorktable->m_dwID;
		Message.m_dwInactiveWorktableID = dwInactiveID;

		Message.Send();
	}
}

void CWorktableManager::Reset()
{
	m_WorktableMap.clear();
	m_pActiveWorktable = NULL;
}

